package com.kalix.dataPlatform.right.biz;

import com.kalix.dataPlatform.right.api.biz.ILineChartRequestBeanService;
import com.kalix.dataPlatform.right.api.dao.ILineChartRequestBeanDao;
import com.kalix.dataPlatform.right.api.model.PostRequestUtil;
import com.kalix.dataPlatform.right.entities.LineChartRequestBean;
import com.kalix.dataPlatform.right.entities.LineChartTitleBean;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 功能描述:
 *
 * @Description: 折线图请求路径 专线宽带占用比 服务层实现
 * @Author: yyp
 * @CreateDate: 2019/6/12$ 09:26$
 * @Version:
 */
public class LineChartRequestBeanServiceImpl extends GenericBizServiceImpl<ILineChartRequestBeanDao, LineChartRequestBean> implements ILineChartRequestBeanService {
    public LineChartRequestBeanServiceImpl() {
        super.init(LineChartRequestBean.class.getName());
    }
    public static String Ip = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String Port =(String) ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");
    /**
     * 功能描述:
     *
     * @Description: 获取折线图服务的数据
     * @Author: yyp
     * @CreateDate: 2019/6/12$ 09:42$
     * @Version:
     */
    @Override
    public String getLineChartRequestValue() {
        String sql = "select li from LineChartRequestBean li";
        List<LineChartRequestBean> list = dao.find(sql);
        // 时间是取当前时间前
        long ms = System.currentTimeMillis();
        JSONObject obj = new JSONObject();
        JSONArray time = new JSONArray();
        JSONArray values = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            JSONArray timeVal = new JSONArray();
            JSONArray valueVal = new JSONArray();
            for (int j = 5; j > 0; j--) {
                long startTime = ms - 7200000 * j;
                long endTime = ms - 7200000 * (j - 1);
                Date star_Time = new Date();
                Date end_Time = new Date();
                star_Time.setTime(startTime);
                end_Time.setTime(endTime);
                DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String start = dateformat.format(star_Time);
                String end = dateformat.format(end_Time);
                String body = "{" +
                        "\"metrics\": [{" +
                        "\"moType\": \"Line\"," +
                        "\"uuid\": \"" + list.get(i).getUuid() + "\"," +
                        "\"metric\": \"ifPercent\"" +
                        "}]," +
                        "\"startTime\": \"" + start + "\"," +
                        "\"endTime\": \"" + end + "\"" +
                        "}";
                String json = PostRequestUtil.HttpPostWithJson("http://"+Ip+":"+Port+"/api/v1/record/data", body);
                //取出返回数据需要的值
                JSONObject jsonObject = new JSONObject(json);
                JSONArray results = jsonObject.getJSONArray("results");
                JSONObject jj = results.getJSONObject(0);
                JSONArray value = jj.getJSONArray("values");
                JSONArray first = value.getJSONArray(0);
                String v = first.getString(1);
                timeVal.put(start);
                valueVal.put(v);
            }
            time.put(timeVal);
            values.put(valueVal);
            obj.put("time", time);
            obj.put("value", values);
        }
        return obj.toString();
    }

    public List<LineChartTitleBean> getListTitle() {
        String sql = "select lt from LineChartTitleBean lt";
        List<LineChartTitleBean> list = dao.find(sql);
        return list;
    }
}
